package com.ic.controller;

import com.ic.base.BaseController;
import com.ic.base.OperatorType;
import com.ic.base.PageInfo;
import com.ic.entity.Subject;
import com.ic.service.SubjectService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * Description: 物品科目控制层类
 * Author: spark
 * Date  : 2017/6/25
 * Time  : 18:00.
 */
@Controller
@RequestMapping("/subjectController")
public class SubjectController extends BaseController {
	@Resource
	private SubjectService subjectService;

	/**
	 * 根据查询条件获取物品科目信息
	 *
	 * @param subject  物品科目
	 * @param pageInfo 分页信息
	 * @return 返回Map
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/showSubjectList")
	@ResponseBody
	public Map showSubjectList(Subject subject, PageInfo pageInfo) {
		return success(subjectService.showSubjectList(subject, pageInfo), OperatorType.SELECT, null);
	}

	/**
	 * 检查物品科目名称是否已存在
	 *
	 * @param subject 物品科目
	 * @return 返回Map
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/checkSubjectName")
	@ResponseBody
	public Map checkSubjectName(Subject subject) {
		return success(subjectService.checkSubjectName(subject), OperatorType.SELECT, null);
	}

	/**
	 * 添加物品科目信息
	 *
	 * @param subject 物品科目
	 * @return 返回Map
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/addSubject")
	@ResponseBody
	public Map addSubject(Subject subject) {
		subjectService.addSubject(subject);
		return success(new HashMap(), OperatorType.ADD, null);
	}

	/**
	 * 根据物品科目ID更改物品科目状态
	 *
	 * @param ids 物品科目ID
	 * @return 返回Map
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/updateState")
	@ResponseBody
	public Map updateState(String ids, String state) {
		subjectService.updateState(ids.split(","), state);
		return success(new HashMap(), OperatorType.UPDATE, null);
	}

	/**
	 * 根据ID更新物品科目信息
	 *
	 * @param subject 物品科目
	 * @return 返回Map
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/updateSubject")
	@ResponseBody
	public Map updateSubject(Subject subject) {
		subjectService.updateByPrimaryKeySelective(subject);
		return success(new HashMap<>(), OperatorType.UPDATE, null);
	}

	/**
	 * 根据物品科目ID查询物品科目信息
	 *
	 * @param id 物品科目ID
	 * @return 返回Map
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/showSubject")
	@ResponseBody
	public Map showSubject(String id) {
		return success(subjectService.showSubject(id), OperatorType.SELECT, null);
	}
}
